package processor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import Util.SimpleLogger;

import parse.GameParser;
import parse.TournamentParser;
import printer.ResultPrinter;
import data.Game;
import data.GameList;
import data.Player;
import data.TournamentList;

public class CalendarProcessor {
	
	private TournamentList tournamentList = null;
	private GameList gameList = null;
	
	private TournamentParser tParser = null;
	private GameParser gParser = null;
	
	private String tFileName;
	private String gFileName;
	private String rFileName;
	
	public CalendarProcessor(String tournamentFName, String gamesFName, String resultFileName){
		tFileName = tournamentFName;
		gFileName = gamesFName;
		rFileName = resultFileName;
	}
	
	public void process(){
		SimpleLogger.log ("change property", "tFileName: " + tFileName);
		SimpleLogger.log ("change property", "gFileName: " + gFileName);
		SimpleLogger.log ("change property", "rFileName: " + rFileName);
		
		SimpleLogger.log ("init", "Init tournament list Parser");		
		tParser = new TournamentParser(tFileName);
		
		SimpleLogger.log ("parse", "Start parsing tournament list");
		tournamentList = tParser.parse ();
		
		gParser = new GameParser (gFileName, tournamentList);
		gameList = gParser.parse ();
		
		Collections.sort(tournamentList.getPlayers (), new CustomComparator());
        GameList aList = generateTour(tournamentList);
        
        ResultPrinter.printResultList (tournamentList, rFileName);
        ResultPrinter.printNextTour (aList, gFileName);
        return;
	}
		
	private GameList generateTour(TournamentList tList){
		GameList res = new GameList();
		for (int i = 0; i < tList.size () - 1; i++){
            Player gamer = tList.getPlayers ().get (i);
            if (gamer.isFree()){
                res.addGame(generateGame(i,gamer, tList));
            }
		}
		
		return res;
	}
	
	private Game generateGame(int index, Player player, TournamentList tList){
		ArrayList<Player> res = new ArrayList<Player>();
        res.add(player);
		for (int j = index + 1; j < tList.size (); j++){
            Player p = tList.getPlayers ().get (j);
            boolean flag = p.isFree ()&&p.canPlay (player);
			if (flag){
				if (res.size() == 2){
					if (!p.canPlay (res.get (1))){
						continue;
					}
				}
                p.setBusy();
                res.add(p);                
                if (res.size() == 3){
                    return new Game(res.toArray(new Player[0]),new Double[]{0.0,0.0,0.0});
                }
			}
		}
		
		
		return null;
	}

    private class CustomComparator implements Comparator<Player> {
        @Override
        public int compare(Player p1, Player p2) {
            if (p1.points () < p2.points ()){
                return 1;
            }
            if (p1.points () == p2.points ()){
                return 0;
            }
            if (p1.points () > p2.points ()){
                return -1;
            }
            return 0;
        }
    }
}
